package com.example.thirdannotation.y2024.bean;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 实现了ApplicationContextAware接口后，可以通过setApplicationContext，将spring容器作为类成员变量保存。
 */
@Component
public class Dog implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public Dog(){
        System.out.println("dog ... constructor ...");
    }

    // 对象创建并赋值之后调用
    @PostConstruct
    public void init(){
        System.out.println("dog ... PostConstruct ...");
    }

    // 容器移除对象之前调用
    @PreDestroy
    public void destroy(){
        System.out.println("dog ... PreDestroy ...");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
